package com.techfort.testguice.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.techfort.testguice.pojo.Bookmark;

public class BookmarkDAO implements Crud<Bookmark> {
	private static final String DATABASE_TABLE = "bookmarks";
	private static final String TITLE = "title";
	private static final String TIME = "time";
	private final Context context;
	private static final String TAG = "INFO";
	private static final String CREATE = 
		"create table bookmarks (title text not null, time int unsigned )";
	
	private DatabaseHelper dh;
	private SQLiteDatabase db;
	
	public BookmarkDAO(Context ctx){
		this.context = ctx;
		dh = new DatabaseHelper(context);
		db = dh.getWritableDatabase();
		
	}
	@Override
	public void delete(Bookmark object) {
		delete(object.getTitle());
	}

	@Override
	public Bookmark get(String title) {
		Cursor cur = getTitle(title);
		return new Bookmark(cur.getString(1),cur.getInt(2));
	}

	@Override
	public List<Bookmark> getAll() {
		List<Bookmark> bms = new ArrayList<Bookmark>();
		
		Cursor cur = getAllTitles();
		while(cur.isAfterLast()==false){
			bms.add(new Bookmark(cur.getString(1),cur.getInt(1)));
			cur.moveToNext();
		}
		return bms;
	}
	@Override
	public void store(Bookmark object) {
		insertTitle(object.getTitle(),object.getTime() );
	}
	
	
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        public DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                              int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                  + " to "
                  + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }   
    
    
    public BookmarkDAO open() throws SQLException 
    {
        db = dh.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        db.close();
    }
    
    //---insert a title into the database---
    public long insertTitle(String title,int time) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(TIME, time);
        initialValues.put(TITLE, title);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular title---
    public boolean delete(String title) 
    {
        return db.delete(DATABASE_TABLE, TITLE + 
        		"=" + title, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllTitles() 
    {
        return db.query(DATABASE_TABLE, new String[] { TITLE, TIME}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular title---
    public Cursor getTitle(String title) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                		TITLE,
                		TIME
                		}, 
                		TITLE + "=" + title, 
                		null,
                		null, 
                		null, 
                		null, 
                		null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(int time, String title) 
    {
        ContentValues args = new ContentValues();
        args.put(TITLE, title);
        args.put(TIME, time);
        
        return db.update(DATABASE_TABLE, args, 
                         TITLE + "=" + title, null) > 0;
    }
	
}
